﻿using System;
using System.Windows.Forms;
using System.Drawing;

namespace FIS.iDET.TemplateCreator
{
    public partial class FormEdit : Form
    {
        public FormEdit()
        {
            InitializeComponent();
        }

        private FormTemplateCreator parentForm;
        private bool editMode = true;

        public FormEdit(FormTemplateCreator form, bool editMode)
        {
            InitializeComponent();
            this.parentForm = form;
            this.editMode = editMode;
            if (!editMode)
            {
                createButton.Text = "Create Field";
                this.Text = "Create new field";
                comboBoxDataType.SelectedIndex = 2;
            }
            else
            {
                int i = parentForm.dataGridView.SelectedCells[0].ColumnIndex;
                textBoxFieldName.Text = parentForm.dataGridView.Columns[i].HeaderText;
                comboBoxDataType.Text = parentForm.dataGridView.Rows[0].Cells[i].Value.ToString();
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            if (editMode)
            {
                // if user input data is valid
                if (textBoxFieldName.Text != "" && comboBoxDataType.SelectedIndex > -1)
                {
                    // get index of selected column
                    int i = parentForm.dataGridView.SelectedCells[0].ColumnIndex;

                    // edit field name, ie column header text
                    parentForm.dataGridView.Columns[i].HeaderText = textBoxFieldName.Text;

                    // set new data type to row
                    parentForm.dataGridView.Rows[0].Cells[i].Value = comboBoxDataType.Text;

                    this.Close();
                }
                else
                {
                    MessageBox.Show("Please enter valid values for Field Name and Data Type");
                }
            }
            else
            {
                // if user input data is valid
                if (textBoxFieldName.Text != "" && comboBoxDataType.SelectedIndex > -1)
                {
                    // add new column
                    int i = parentForm.dataGridView.Columns.Add((parentForm.count++).ToString(), textBoxFieldName.Text.Trim());
                    parentForm.dataGridView.Columns[i].ToolTipText = "Drag to re-order this field";
                    parentForm.dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    // add new column element in Column list
                    GlobalVariables.columns.Add(new DataColumn(textBoxFieldName.Text.Trim(), comboBoxDataType.SelectedIndex, 100, new DoubleRectangle(0, 0, 0, 0)));

                    // add one new row if there is none
                    if (parentForm.dataGridView.RowCount == 0)
                        parentForm.dataGridView.Rows.Add();

                    // set data type to row
                    parentForm.dataGridView.Rows[0].Cells[i].Value = comboBoxDataType.Text;
                    parentForm.dataGridView.Rows[0].Cells[i].ToolTipText = "Double click to edit this Data type";

                    textBoxFieldName.Text = "";
                }
                else
                {
                    MessageBox.Show("Please enter valid values for Field Name and Data Type");
                }
            }
        }

        private void textBoxFieldName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Enter)
            {
                createButton_Click(sender, null);
            }
            else if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
        }

    }
}